package com.mane.controller;

import com.mane.entity.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Date;
import java.util.List;

public class TestJWT {
    public static void main(String[] args) throws IOException {
        createJWT();

        parser();
    }

    public static void createJWT(){
        // 1. 创建一个jwt令牌的构建者
        JwtBuilder jwtBuilder = Jwts.builder();
        // 2. 设置公共的载荷数据
        jwtBuilder.setIssuer("woniu");
        jwtBuilder.setSubject("study");
        jwtBuilder.setId("woniu");
        jwtBuilder.setIssuedAt(new Date());
        // 3. 设置加密方式及秘钥
        jwtBuilder.signWith(SignatureAlgorithm.HS256, "dfrdeasd123456hdsahdasjkdhasjdhsajdhasjdhaas");
        // 4. 生成jwt令牌
        String token = jwtBuilder.compact();
        System.out.println("生成的token为:" + token);
    }

    public static void parser() throws IOException {
        // 1. 定义token
        String toke = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3b25pdSIsInN1YiI6InN0dWR5IiwianRpIjoid29uaXUiLCJpYXQiOjE3Mzk3NzY0Nzl9.BpUPF7BZcmyFuCcg6ViMFty4O8LkqYZ8YruLvFD04UM";
        // 2. 解析token
        Claims claims = Jwts.parser().setSigningKey("dfrdeasd123456hdsahdasjkdhasjdhsajdhasjdhaas").parseClaimsJws(toke).getBody();
        // 3. 获取内容
        System.out.println(claims.toString());
    }

}
